package com.walker.simple;

import com.rabbitmq.client.*;
import com.walker.BaseConfig;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description:
 * @ProjectName: walker-step
 * @Package: com.walker.simple
 * @ClassName: SimpleRecv
 * @Author: yangyl
 * @CreateDate: 2020/6/20 17:15
 */
public class SimpleRecv {


    private final static String QUEUE_NAME = "hello";

    public static ConnectionFactory getConnectionFactory() {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(BaseConfig.HOST);
        connectionFactory.setVirtualHost(BaseConfig.VHOST);
        connectionFactory.setPort(5672);
        connectionFactory.setUsername(BaseConfig.USERNAME);
        connectionFactory.setPassword(BaseConfig.PASSWORD);
        return connectionFactory;
    }

    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = getConnectionFactory();
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, true, false, false, null);

        System.out.println("start Rece info.. client3");

        //todo 声明回调处理类
        DeliverCallback deliverCallback = new DeliverCallback() {
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] body = message.getBody();
                System.out.println("receive3 :" + new String(body));
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("处理完了...");
            }
        };

        channel.basicConsume(QUEUE_NAME, true,(consumerTag,message)->{

        }, consumerTag -> {
        });

    }
}
